unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Btnx_1: TButton;
    Btnx_2: TButton;
    Btnx_3: TButton;
    Btnx_4: TButton;
    procedure Btnx_1Click(Sender: TObject);
    procedure Btnx_2Click(Sender: TObject);
    procedure Btnx_3Click(Sender: TObject);
    procedure Btnx_4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  hProcess:THandle;  

implementation

uses
  ShellAPI,Class_KzUtils;

{$R *.dfm}


function MyFunction(p:Pointer):DWORD;stdcall;
begin
  if WaitForSingleObject(hProcess,INFINITE) = WAIT_OBJECT_0  then
  begin
    //Form1.edt1.Text := Format('?? %d ??',[hProcess]);
    Form1.Caption:='done';
  end;
  Result := 0;
end;

procedure TForm1.Btnx_1Click(Sender: TObject);
var
  I:Integer;
begin
  //ShellExecute(Application.Handle,'open',PChar(TKzUtils.ExePath+'someword.doc'),);
  I:=ShellExecute(Self.Handle,'open',pchar(TKzUtils.ExePath+'someword.doc'),'-s','',sw_shownormal);
end;

procedure TForm1.Btnx_2Click(Sender: TObject);
var
  ExecuteInfo: SHELLEXECUTEINFO;
  ThreadID:DWORD;
begin
  ZeroMemory(@ExecuteInfo, SizeOf(ExecuteInfo));
  ExecuteInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  ExecuteInfo.cbSize := sizeof(ExecuteInfo);
  ExecuteInfo.lpVerb := 'open';
  ExecuteInfo.lpFile := PChar(Format('%S',[TKzUtils.ExePath+'someword.doc']));;
  ExecuteInfo.lpParameters := nil;
  ExecuteInfo.lpDirectory := nil;
  ExecuteInfo.nShow := SW_NORMAL;
  if ShellExecuteEx(@ExecuteInfo) then
  begin
    hProcess := ExecuteInfo.hProcess;
    //self.edt1.Text := Format('process %d starting...',[hProcess]);
    self.Caption:=Format('process %d starting...',[hProcess]);
    CreateThread(nil,0,@MyFunction,nil,0,ThreadID);  
  end;
end;


procedure TForm1.Btnx_3Click(Sender: TObject);
var
  pInfo:TProcessInformation;
  sInfo:TStartupInfo;
  Path:array[0..MAX_PATH-1] of Char;
  ThreadID:DWORD;
begin
  GetSystemDirectory(Path,MAX_PATH);
  StrCat(Path,'\notepad.exe');
  FillChar(sInfo,SizeOf(sInfo),0);

  Path:='D:\Program Files (x86)\WPS Office\9.1.0.4855\office6\wps.exe';
  if CreateProcess(Path,nil,nil,nil,False,0,nil,nil,sInfo,pInfo) then
  begin
    hProcess := pInfo.hProcess;
    //self.edt1.Text := Format('process %d starting...',[hProcess]);
    self.Caption:=Format('process %d starting...',[hProcess]);
    CreateThread(nil,0,@MyFunction,nil,0,ThreadID);
  end;

end;

procedure TForm1.Btnx_4Click(Sender: TObject);
var
  ExecuteInfo: SHELLEXECUTEINFO;
  ThreadID:DWORD;
begin
  ZeroMemory(@ExecuteInfo, SizeOf(ExecuteInfo));
  ExecuteInfo.fMask := SEE_MASK_NOCLOSEPROCESS;
  ExecuteInfo.cbSize := sizeof(ExecuteInfo);
  ExecuteInfo.lpVerb := 'open';
  ExecuteInfo.lpFile := PChar(Format('%S',[TKzUtils.ExePath+'someword.doc']));
  ExecuteInfo.lpParameters := nil;
  ExecuteInfo.lpDirectory := nil;
  ExecuteInfo.nShow := SW_SHOW;
  if ShellExecuteEx(@ExecuteInfo) then
  begin
    hProcess := ExecuteInfo.hProcess;
    //self.edt1.Text := Format('process %d starting...',[hProcess]);
    self.Caption:=Format('process %d starting...',[hProcess]);
    CreateThread(nil,0,@MyFunction,nil,0,ThreadID);  
  end;
end;

end.
